home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / snpd0493.zip / MOON_AGE.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  3KB  |  97 lines

  1. /* PD by Michelangelo Jones, 1:1/124. */
  2.  
  3. /*
  4. **  Returns 0 for new moon, 14 for full moon,
  5. **  27 for the day before new, and so forth.
  6. */
  7.  
  8. /*
  9. **  This routine sometimes gets "off" by a few days,
  10. **  but is self-correcting.
  11. */
  12.  
  13. int moon_age(int month, int day, int year)
  14. {
  15.       static short int ages[] =
  16.             {18, 0, 11, 22, 3, 14, 25, 6, 17,
  17.             28, 9, 20, 1, 12, 23, 4, 15, 26, 7
  18.             };
  19.       int row;
  20.  
  21.       if (day == 31)
  22.             day = 1;
  23.       switch (month)
  24.       {
  25.       case 1:
  26.             row = day - 1;
  27.             break;
  28.       case 2:
  29.       case 4:
  30.             row = (day + 1) % 30;
  31.             break;
  32.       case 3:
  33.             row = day % 30;
  34.             break;
  35.       case 5:
  36.             row = (day + 2) % 30;
  37.             break;
  38.       case 6:
  39.             row = (day + 3) % 30;
  40.             break;
  41.       case 7:
  42.             row = (day + 4) % 30;
  43.             break;
  44.       case 8:
  45.             row = (day + 5) % 30;
  46.             break;
  47.       case 9:
  48.       case 10:
  49.             row = (day + 7) % 30;
  50.             break;
  51.       case 11:
  52.       case 12:
  53.             row = (day + 9) % 30;
  54.       }
  55.  
  56.       return ((ages[(year + 1) % 19] + row + (year < 1900)) % 30);
  57.  
  58. }
  59.  
  60. #ifdef TEST
  61.  
  62. #include <stdio.h>
  63. #include <stdlib.h>
  64.  
  65. static char *description[] = {
  66.       "new",                  /* totally dark                         */
  67.       "waxing crescent",      /* increasing to full & quarter light   */
  68.       "in its first quarter", /* increasing to full & half light      */
  69.       "waxing gibbous",       /* increasing to full & > than half     */
  70.       "full",                 /* fully lighted                        */
  71.       "waning gibbous",       /* decreasing from full & > than half   */
  72.       "in its last quarter",  /* decreasing from full & half light    */
  73.       "waning crescent"       /* decreasing from full & quarter light */
  74.       };
  75.  
  76. int main(int argc, char *argv[])
  77. {
  78.       int month, day, year, phase;
  79.  
  80.       if (4 > argc)
  81.       {
  82.             puts("Usage: MOON_AGE month day year");
  83.             return EXIT_FAILURE;
  84.       }
  85.       month = atoi(argv[1]);
  86.       day   = atoi(argv[2]);
  87.       year  = atoi(argv[3]);
  88.       if (100 > year)
  89.             year += 1900;
  90.       printf("moon_age(%d, %d, %d) returned %d\n", month, day, year,
  91.             phase = moon_age(month, day, year));
  92.       printf("Moon phase on %d/%d/%d is %s\n", month, day, year,
  93.             description[(int)((phase + 2) * 16L / 59L)]);
  94. }
  95.  
  96. #endif /* TEST */
  97.